#include "conversationtable.h"
#include "sqlitemanager.h"
#include "appchat/accountmanager.h"
#include "appchat/chatmanager.h"

#include <QDebug>
#include <QSqlQuery>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlRecord>

namespace Sqlite {

ConversationTable::ConversationTable(QObject *parent) :
    QObject(parent)
{
}

void ConversationTable::createTable()
{
    QString str = "CREATE TABLE IF NOT EXISTS Conversation (conversationId INTEGER PRIMARY KEY,";
    str.append("uid INTEGER , ");
    str.append("fid INTEGER , ");
    str.append("conveserType INTEGER , ");
    str.append("countMsgNew INTEGER)");

    QSqlQuery qry;
    qry.prepare(str );
    SqliteManager::instance()->doProcessSQLQuery(qry);
}

void ConversationTable::insertOrUpdateConversation(const Converse::AbstractConversation *conversation)
{
    QString str = QString("INSERT OR REPLACE INTO Conversation (conversationId,");
    str.append("uid, ");
    str.append("fid, ");
    str.append("conveserType, ");
    str.append("countMsgNew)");

    str.append("VALUES (%1, %2, %3, %4, %5)");


    QString command = QString(str)
            .arg(conversation->id())
            .arg(AccountManager::instance()->getUserId())
            .arg(ChatManager::instance()->activeFid())
            .arg(conversation->type())
            .arg(-1);

    qDebug() << "++++++++++++++ConversationTable::insertOrUpdateConversation" << command;

    QSqlQuery qry;
    qry.prepare(command);
    SqliteManager::instance()->doProcessSQLQuery(qry);
}

void ConversationTable::clearTable()
{

}

}
